//
//  e7percent.cpp
//  e7engine
//
//  Created by cc on 12-2-28.
//  Copyright (c) 2012年 cc. All rights reserved.
//

#include "e7percent.h"

namespace e7engine {

CE7Percent::CE7Percent()
{
    m_bIsRandom = false ;
    m_pValues = NULL ;
    m_nIndex = 0 ;
    m_nPercent = 0 ;
    m_nMax = 0 ;
}

CE7Percent::~CE7Percent()
{
    SAFE_DELETE_ARR(m_pValues) ;
}

bool CE7Percent::InitPercent(int percent, int max, bool israndom)
{
    ::srand((int)time(0)) ;

    m_bIsRandom = israndom ;

    SAFE_DELETE_ARR(m_pValues) ;
    m_pValues = new int[max] ;

    m_nPercent = percent ;
    m_nMax = max ;

    Reset() ;

    return true ;
}

int CE7Percent::GetValue()
{
    if (m_nIndex >= m_nMax)
    {
        Reset() ;
    }

    return m_pValues[m_nIndex++] ;
}

void CE7Percent::Reset()
{
    if (m_bIsRandom)
    {
        int i ;
        for (i = 0; i < m_nMax; ++i)
        {
            m_pValues[i] = rand()%m_nMax ;
        }
        return ;
    }
    
    int i ;
    for (i = 0; i < m_nMax; ++i)
    {
        m_pValues[i] = i ;
    }
    
    int tindex_s, tindex_e, temp ;
    for (i = 0; i < m_nMax; ++i)
    {
        tindex_s = rand()%m_nMax ;
        tindex_e = rand()%m_nMax ;
        
        temp = m_pValues[tindex_s] ;
        m_pValues[tindex_s] = m_pValues[tindex_e] ;
        m_pValues[tindex_e] = temp ;
    }
    
    m_nIndex = 0 ;
}

}















